<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <el-row :gutter="20" class="model">
        <el-col :xs="8" :sm="8" :md="7" :lg="6" :xl="4">
          <el-card shadow="hover">
            <span>预存款合计</span>
            <div><span>￥ </span> {{ info.total_money }}</div>
          </el-card>
        </el-col>
        <el-col :xs="8" :sm="8" :md="7" :lg="6" :xl="4">
          <el-card shadow="hover">
            <span>预存款月新增</span>
            <div><span>￥ </span> {{ info.month_money }}</div>
          </el-card>
        </el-col>
        <el-col :xs="8" :sm="8" :md="7" :lg="6" :xl="4">
          <el-card shadow="hover">
            <span>预存款月消耗</span>
            <div><span>￥ </span> {{ info.month_use_money }}</div>
          </el-card>
        </el-col>
      </el-row>
      <div class="header">
        <div>
          <el-select v-model="searchTerms.type" placeholder="类型" class="select" clearable @change="getData">
            <el-option :value="1" label="增加"/>
            <el-option :value="2" label="减少"/>
          </el-select>
          <el-input v-model="searchTerms.searchValue" placeholder="请输入内容" class="search-input" @change="getData" />
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
        </div>
        <column-controller :check-items="th" style="float:right;" @change="columnCheck" />
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" min-width="70" header-align="center" align="center" sortable />
        <el-table-column v-if="th[0].check" prop="name" label="客户名称" min-width="120" header-align="center" align="center" />
        <el-table-column v-if="th[1].check" prop="mobile" label="手机" min-width="120" header-align="center" align="center" />
        <el-table-column v-if="th[2].check" prop="money" label="支付金额" min-width="110" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.money.indexOf('-')===0?'danger':'success'">{{ scope.row.money }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column v-if="th[3].check" prop="wallet" label="余额" min-width="110" header-align="center" align="center" />
        <el-table-column v-if="th[4].check" prop="operation" label="操作记录" min-width="300" header-align="center" align="center" />
        <el-table-column v-if="th[5].check" prop="remarks" label="备注" min-width="160" />
        <el-table-column v-if="th[6].check" prop="created_at" label="操作时间" min-width="160" header-align="center" align="center" />
        <!--<el-table-column v-if="$store.getters.id===1" label="操作" width="140" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="danger" size="small" icon="el-icon-delete" circle @click="toDelete(scope.row)"/>
          </template>
        </el-table-column>-->
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>
  </div>
</template>

<script>
import { ColumnController } from '../components'
import { handleCurrentChange, handleSizeChange, handleSortChange, columnCheck } from '../../utils/table'
export default {
  components: { ColumnController },
  data() {
    return {
      downloadLoading: false,
      searchTerms: {
        searchValue: '',
        type: null
      },

      info: {},

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 280,

      // 表格相关参数
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: [],
      th: [{ name: '客户名称', check: true },
        { name: '手机', check: true },
        { name: '支付金额', check: true },
        { name: '余额', check: true },
        { name: '操作记录', check: true },
        { name: '备注', check: true },
        { name: '操作时间', check: true }]
    }
  },
  watch: {},
  created() {
    this.getBalance()
    this.getData()
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, columnCheck,
    getBalance() {
      const url = 'finance/balance'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.info = {
          total_money: res.data.total_money,
          month_money: res.data.month_money,
          month_use_money: res.data.month_use_money
        }
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'finance/balance-list'
      const data = {
        page_no: this.currentPage,
        page_size: this.pageSize,
        type: this.searchTerms.type,
        search: this.searchTerms.searchValue
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    toDelete(row) {
      this.$confirm('此操作将删除该资金记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'bonus/delExpectDonus/' + row.id;
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(e => {
          this.$message.error(e.msg + ',请刷新或联系管理员')
        })
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../styles/index.scss';
  .box-1 {
    .model {
      .el-card {
        color: #fff;
        background-color: $panGreen;
        span {
          font-size: 14px;
        }
        div {
          font-size: 24px;
          margin-top: 10px;
        }
      }
    }
    .header {
      @include flex-row(center, space-between);
      flex-wrap: wrap;
      padding-bottom: 15px;
      div {
        @include flex-row(center, flex-start);
        flex-wrap: wrap;
        .do-btn {
          @extend .bg-green;
          margin-left: 6px;
        }
        .search-input {
          width: 240px;
        }
        .select{
          padding-right: 15px;
          width: 160px;
        }
      }
    }
  }
</style>

